package cn.nexttec.noteplayer.ui.adapters

import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import cn.nexttec.noteplayer.R
import cn.nexttec.noteplayer.databinding.ItemTrackBinding
import cn.nexttec.noteplayer.models.TrackSelectionOption

class TrackAdapter (val tracks:List<TrackSelectionOption>, val selected:Int= 0, val onSelected:(TrackSelectionOption, Int)->Unit):RecyclerView.Adapter<TrackAdapter.ViewHolder>(){

    inner class ViewHolder(val binding:ItemTrackBinding):RecyclerView.ViewHolder(binding.root){
        fun bind(track:TrackSelectionOption, isSelected:Boolean){
            binding.tvName.text = track.label
            if (isSelected){
                binding.tvName.setTextColor(ContextCompat.getColor(binding.root.context, R.color.dblue))
            } else {
                binding.tvName.setTextColor(Color.GRAY)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemTrackBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return tracks.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val trace = tracks[position]
        holder.bind(trace, position == selected)
        holder.binding.root.setOnClickListener {
            onSelected(trace, position)
        }
    }


}